package com.admission.web;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.SchoolIntr;
import com.admission.service.SchoolIntrManager;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class SchoolIntrAction extends CrudActionSupport<SchoolIntr> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 154729613736706020L;

	private SchoolIntr entity;
	private String type;
	private SchoolIntrManager schoolIntrManager;

	public SchoolIntr getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(type)) {
			entity = new SchoolIntr();
		} else {
			entity = schoolIntrManager.getByType(type);
		}
	}

	/**
	 * 根据异步传来的type而改变content的内容
	 * 
	 * @return
	 * @throws Exception
	 */
	public String change() throws Exception {
		/*
		 * // 获取JQuery客户端动态生成的callback函数名.
		 * System.out.println(schoolIntrManager.getByType(type).getContent());
		 * // 设置需要被格式化为JSON字符串的内容. Map<String, String> map =
		 * Collections.singletonMap("content",
		 * schoolIntrManager.getByType(type).getContent()); // 渲染返回结果.
		 * Struts2Utils.renderJson(map);
		 */
		if (schoolIntrManager.getByType(type) != null) {
			SchoolIntr si = schoolIntrManager.getByType(type);
			JSONObject jo = new JSONObject();
			jo.put("type", si.getType());
			jo.put("inputTime", si.getInputTime());
			jo.put("content", si.getContent());
			Struts2Utils.renderJson(jo.toString());
		}
		return null;
	}

	public SchoolIntr getEntity() {
		return entity;
	}

	public void setEntity(SchoolIntr entity) {
		this.entity = entity;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Autowired
	public void setSchoolIntrManager(SchoolIntrManager schoolIntrManager) {
		this.schoolIntrManager = schoolIntrManager;
	}

}
